﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using System.Windows.Browser;

namespace Scripting_Engine
{
    public partial class ScriptingEngineUserControl : UserControl
    {
        public ScriptingEngineUserControl()
        {
            InitializeComponent();
        }
        bool IsDirty = false;
        public static string Data = string.Empty;
        private void Open_Click(object sender, RoutedEventArgs e)
        {
            #region OpenDlg
            if (TxtFileName.Text == string.Empty)
            {
                OpenFileDialog OpnDlg = new OpenFileDialog();
                OpnDlg.Multiselect = false;
                if (OpnDlg.ShowDialog() == true)

                    using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (StreamReader reader = new StreamReader(OpnDlg.File.OpenRead()))
                        {
                            string str = string.Empty;
                            while (!reader.EndOfStream)
                            {
                                str = reader.ReadToEnd();
                            }
                            TxtFileData .Text = str;
                            reader.Close();
                        }
                    }

            }
            #endregion
            #region else
            else 
                {
                    String s = TxtFileName.Text;
                    using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(s, FileMode.Open, isoStore))
                        {
                            using (StreamReader reader = new StreamReader(isoStream))
                            {
                                string str = string.Empty;
                                while (!reader.EndOfStream)
                                {
                                    str = reader.ReadToEnd();
                                }
                                TxtFileData.Text = str;
                                reader.Close();
                            }
                        }
                    }
                 }
             #endregion  
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            String FileName= string.Empty;
            FileName = TxtFileName.Text;
            SetData(TxtFileData.Text, FileName);
        }
   
        public static void SetData(string strData, string strFileName)
        {
            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(strFileName, FileMode.Create, isoStore))
                {
                    using (StreamWriter writer = new StreamWriter(isoStream))
                    {
                        writer.Write(strData);
                    }
                }
            }
        }

        public static string GetDataFromTextBox(string FromTxtbx)
        {
            return FromTxtbx;
        }

        private void CloseBtn_Click(object sender, RoutedEventArgs e)
        {
            if (IsDirty == true)
            {
                if (MessageBox.Show("do u want 2 save", "save", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    SaveFile(TxtFileName.Text, TxtFileData.Text);
                }
                HtmlPage.Window.Eval("window.close()");
            }
            else
            {
                HtmlPage.Window.Eval("window.close()");
            }
        }

        private void BSave_Click(object sender, RoutedEventArgs e)
        {
            SaveFile(TxtFileName.Text, TxtFileData.Text);
        }

        private void SaveFile(String FileName, String FileData)
        {
            SetData(FileData, FileName);
        }

        private void TxtFileData_TextChanged(object sender, TextChangedEventArgs e)
        {
            IsDirty = true;
        }
    }
 }
